﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Dashboard.ProductBonus.Business;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win;

namespace Dashboard.ProductBonus
{
    public partial class FormUnproductiveTime : Form
    {
        public FormUnproductiveTime()
        {
            InitializeComponent();
        }

        private void FormUnproductiveTime_Load(object sender, EventArgs e)
        {

            this.SetDefaultDateFromTo();
            this.BindTeamList();
            this.BindReasonCodeList();
        }

        private void BindReasonCodeList()
        {
            this.comboBoxReasonCode.Items.Clear();
            this.comboBoxReasonCode.Items.Add("");

            DataTable reasonList = new BusinessBase().GetAllReasonList();

            for (int i = 0; i < reasonList.Rows.Count; i++)
            {
                if (reasonList.Rows[i]["pcode"].ToString() == "16")
                {
                    continue;
                }
                this.comboBoxReasonCode.Items.Add(reasonList.Rows[i]["pcode"] + "-" + reasonList.Rows[i]["pdesc"]);
            }
        }

        private void toolStripButtonQuery_Click(object sender, EventArgs e)
        {
            QueryData();
        }

        private void QueryData()
        {
            UnproductiveTime at = new UnproductiveTime();
            DataTable result = at.GetUnproductiveTime(this.comboBoxStaffNo.Text == "" ? "" : this.comboBoxStaffNo.Text.Split('-')[0],
                                                      this.dateTimePickerFrom.Value.Date,
                                                      this.dateTimePickerTo.Value.Date,
                                                      this.comboBoxTeam.Text,
                                                      this.comboBoxShift.Text,
                                                      this.comboBoxReasonCode.Text);

            this.ultraGridMain.DataSource = result;
            this.ultraGridMain.DataBind();
        }

        private void toolStripButtonCreate_Click(object sender, EventArgs e)
        {
            using (FormAddNonproductiveTime addForm = new FormAddNonproductiveTime())
            {
                addForm.ShowDialog(this);

                this.QueryData();
            }
        }

        private void toolStripButtonDelete_Click(object sender, EventArgs e)
        {
            this.ultraGridMain.UpdateData();


            List<int> selectedIds = new List<int>();
            foreach (UltraGridRow row in this.ultraGridMain.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Check"].Value) == true)
                {
                    selectedIds.Add(Convert.ToInt32(row.Cells["Id"].Value));
                }
            }

            if (selectedIds.Count == 0)
            {
                MessageBox.Show(this, "No data selected.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (MessageBox.Show(this, "Are you sure to delete selected records?", "Confirm",
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
            {
                try
                {
                    this.Cursor = Cursors.WaitCursor;

                    UnproductiveTime at = new UnproductiveTime();
                    foreach (int id in selectedIds)
                    {
                        at.DeleteUnproductiveTime(id);
                    }

                    MessageBox.Show(this, "Delete successfully.", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    QueryData();
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private void ultraGridMain_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            // 自适应列宽
            e.Layout.AutoFitStyle = Infragistics.Win.UltraWinGrid.AutoFitStyle.ResizeAllColumns;
            // 是否允许调整列宽
            e.Layout.Override.AllowColSizing = AllowColSizing.Free;
            // 是否允许移动栏位顺序
            e.Layout.Override.AllowColMoving = AllowColMoving.NotAllowed;

            // 设置Grid的Split窗口个数，建议设置为1--不允许Split
            e.Layout.MaxColScrollRegions = 1;
            e.Layout.MaxRowScrollRegions = 1;

            // 冻结列
            e.Layout.UseFixedHeaders = false;

            // 排序
            e.Layout.Override.HeaderClickAction = Infragistics.Win.UltraWinGrid.HeaderClickAction.SortSingle;

            // 不允许删除
            e.Layout.Override.AllowDelete = DefaultableBoolean.False;

            // 滚动提示
            e.Layout.Bands[0].ScrollTipField = "StaffNo";

            // 隐藏不需要显示的列
            e.Layout.Bands[0].Columns["ID"].Hidden = true;

            for (int i = 0; i < e.Layout.Bands[0].Columns.Count; i++)
            {
                if (e.Layout.Bands[0].Columns[i].Key != "Check")
                {
                    e.Layout.Bands[0].Columns[i].CellActivation = Activation.NoEdit;
                }
                else
                {
                    e.Layout.Bands[0].Columns[i].CellActivation = Activation.AllowEdit;
                }
            }
        }

        private void toolStripButtonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBoxTeam_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindShift();
        }

        private void comboBoxShift_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.BindStaffNoList();
        }
        private void BindTeamList()
        {
            BusinessBase bb = new BusinessBase();
            DataTable teams = bb.LoadTeamList();

            this.comboBoxTeam.Items.Clear();
            this.comboBoxTeam.Items.Add("");

            foreach (DataRow row in teams.Rows)
            {
                this.comboBoxTeam.Items.Add(row["team"].ToString());
            }

            if (this.comboBoxTeam.Items.Count > 0)
            {
                this.comboBoxTeam.SelectedIndex = 0;
            }

            bb = null;
        }

        private void BindShift()
        {
            BusinessBase bb = new BusinessBase();
            DataTable result = bb.LoadTeamShiftList(this.comboBoxTeam.Text);

            this.comboBoxShift.Items.Clear();
            this.comboBoxShift.Items.Add("");

            foreach (DataRow row in result.Rows)
            {
                this.comboBoxShift.Items.Add(row["Shift"].ToString());
            }
            if (this.comboBoxShift.Items.Count > 0)
            {
                this.comboBoxShift.SelectedIndex = 0;
            }
        }

        private void BindStaffNoList()
        {
            BusinessBase bb = new BusinessBase();
            DataTable staffs = bb.GetStaffOfTeamShift(this.comboBoxTeam.Text, this.comboBoxShift.Text);

            this.comboBoxStaffNo.Items.Clear();
            this.comboBoxStaffNo.Text = string.Empty;
            this.comboBoxStaffNo.Items.Add("");

            foreach (DataRow row in staffs.Rows)
            {
                this.comboBoxStaffNo.Items.Add(row["staffno"].ToString() + "-" + row["name"].ToString());
            }
            if (this.comboBoxStaffNo.Items.Count > 0)
            {
                this.comboBoxStaffNo.SelectedIndex = 0;
            }
            bb = null;
        }
        private void SetDefaultDateFromTo()
        {
            var today = DateTime.Today.Date;
            var monday = today.AddDays(-((int)DateTime.Now.DayOfWeek == 0 ? 7 : (int)DateTime.Now.DayOfWeek)).AddDays(1);
            this.dateTimePickerFrom.Value = monday;
        }

    }
}
